import axios from "axios";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

// 创建 axios 实例
const httpInstace = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 5000,
});

// 请求拦截器
httpInstace.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
httpInstace.interceptors.response.use(
  (response) => {
    const { code, data, message } = response.data;

    if (code === 200) {
      return data;
    } else if (code === 204) {
      return null;
    } else {
      console.log(message);
      ElMessage.error(message || '网络错误');
      return Promise.reject(message || '网络错误');
    }
  },
  (error) => {
    ElMessage.error('网络错误');
    return Promise.reject(error);
  }
);

export default httpInstace;
